package com.nip.workmanager.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.nip.workmanagement.R;
import com.nip.workmanager.adapter.CheckListAdapter;
import com.nip.workmanager.common.utilities.UtilitiesDataBase;
import com.nip.workmanager.database.CheckListTable;
import com.nip.workmanager.database.TodoTable;
import com.nip.workmanager.entity.CheckListEntity;
import com.nip.workmanager.entity.ListViewCheckListEntity;
import com.nip.workmanager.entity.TodoEntity;

public class CheckListActivity extends Activity {

	private Button _btnAdd;
	private ListView _lvCheckList;
	private CheckListTable _checkListTable;
	private TodoTable _todoTable;
	private List<ListViewCheckListEntity> _mSources;
	private CheckListAdapter _mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.check_list_activity);
		UtilitiesDataBase.CopyDataBase(CheckListActivity.this);
		Initialize();
		RefreshList();
	}

	

	/**
	 * Initialize variables
	 */
	private void Initialize() {

		_btnAdd = (Button) findViewById(R.id.btnAddWork);
		_btnAdd.setOnClickListener(new MyEvent());

		_mSources = new ArrayList<ListViewCheckListEntity>();
		_lvCheckList = (ListView) findViewById(R.id.lvCheckList);
		_lvCheckList.setOnItemClickListener(new MyEvent());
		_mAdapter = new CheckListAdapter(CheckListActivity.this, _mSources);
		_lvCheckList.setAdapter(_mAdapter);

		_checkListTable = new CheckListTable(CheckListActivity.this);
		_todoTable = new TodoTable(CheckListActivity.this);
	}

	/**
	 * Show activity add a work
	 */
	private void AddWorkToCheckList() {
		Intent i = new Intent(CheckListActivity.this,
				AddCheckListActivity.class);
		startActivity(i);
	}

	/**
	 * 
	 */
	private void RefreshList() {
		ArrayList<CheckListEntity> listTeam = _checkListTable
				.GetAllWorkInCheckList();

		if (listTeam != null) {
			
			List<TodoEntity> listTodo;
			ListViewCheckListEntity listViewCheckListEntity;
			ArrayList<Integer> totalTodo;
			_mSources.clear();
			
			for (CheckListEntity checkListEntity : listTeam) {
				listViewCheckListEntity = new ListViewCheckListEntity();
				listTodo = _todoTable.GetToDoByWorkId(checkListEntity
						.getWorkId());
				totalTodo = CoutTodoByStatus(listTodo);
				listViewCheckListEntity.setWorkId(checkListEntity.getWorkId());
				listViewCheckListEntity.setIcon(checkListEntity.getIcon());
				listViewCheckListEntity.setWorkName(checkListEntity
						.getWorkName());
				if (totalTodo == null) {
					listViewCheckListEntity.setNotification("(No Items)");
				} else if (totalTodo.get(1) == 0) {
					listViewCheckListEntity.setNotification("All done !");
				} else {
					listViewCheckListEntity.setNotification(totalTodo.get(0)
							+ " Remaning");
				}
				_mSources.add(listViewCheckListEntity);
			}
			_mAdapter.notifyDataSetChanged();
		}
	}

	/**
	 * @param listTodo
	 * @return
	 */
	private ArrayList<Integer> CoutTodoByStatus(List<TodoEntity> listTodo) {
		ArrayList<Integer> listResponse = null;
		int totalOn = 0;
		int totalOff = 0;
		if (listTodo != null && listTodo.size() > 0) {
			listResponse = new ArrayList<Integer>();
			for (TodoEntity todo : listTodo) {
				if (todo.getToDoStatus()){
					totalOn++;
				} else {
					totalOff++;
				}
			}
			listResponse.add(totalOn);
			listResponse.add(totalOff);
		}
		return listResponse;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		RefreshList();
	}
	/**
	 * Contain even of class
	 * 
	 * @author ThangNM
	 * 
	 */
	private class MyEvent implements OnClickListener, OnItemClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btnAddWork:
				AddWorkToCheckList();
				break;

			default:
				break;
			}
		}

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub

		}
		
	}
}
